﻿import mx.utils.Delegate;
import arkadium.XMLFile;

//This class loads an XML file and recursively converts the XML elements into strings that are dynamically attached to this object which can be retrieved at a later time.

class arkadium.XMLConfigFile
{
	private var _fCallBackFunction:Function;
	
	public function XMLConfigFile()
	{
		
		
	}
	
	public function loadConfigFile( oCallerObject:Object, oCallerFunction:Function, sFileURL:String )
	{
		_fCallBackFunction = Delegate.create( oCallerObject, oCallerFunction );
		
		var xXMLFile:XMLFile = new XMLFile();
		xXMLFile.loadFile( this, configFileLoaded, sFileURL );
	}
	
	private function configFileLoaded( xXML:XML, bSuccess:Boolean )
	{
		if ( bSuccess )
		{
			processXMLData( xXML.firstChild );
		}
		
		
		_fCallBackFunction.apply( null, [ bSuccess ] );
		
		delete xXML;
	}
	
	private function processXMLData( xNode:XMLNode )
	{
		// If this node is a TEXT_NODE, save it's data.
		if ( xNode.nodeType == 3 )
		{
			saveElementData( xNode );
		}
		
		// If this node is a ELEMENT_NODE, process it's children nodes recursively.
		else if ( xNode.nodeType == 1 )
		{
			for ( var i:Number = 0; i < xNode.childNodes.length; ++i )
			{
				processXMLData( xNode.childNodes[ i ] );
			}
		}
	}
	
	private function saveElementData( xNode:XMLNode )
	{
		this[ xNode.parentNode.nodeName ] = new String( xNode.nodeValue );
		
		//This function should not be taken advantage of, it is sloppy and saves values to the root,
		//however, since this is the way that the old games did it, we will keep doing this for consistancy
		_level0[ xNode.parentNode.nodeName ] = new String( xNode.nodeValue );
	}

	public function getData( sVariableName:String ):String
	{
		return String( this[ sVariableName ] );
	}

	public function setData (sName:String, varValue){
		this[sName] = varValue
	}

	
}
